<form class="layui-form seller-form" action="">
	<div class="layui-form-item">
		<div class="layui-inline">
			<button class="layui-btn add-class layui-btn-sm"><i class="layui-icon">&#xe608;</i>添加</button>
		</div>
	</div>
</form>
<div class="table-body">
	<table id="typeTable" lay-filter="test"></table>
</div>
<script>
    var table;
    layui.use(['form', 'layedit', 'laydate','table'], function(){
        var form = layui.form;
        table = layui.table.render({
            elem: '#typeTable',
            height: 'full-99',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            url: "{:url('GoodsTypeSpec/index')}?_ajax=1",
            id: 'typeTable',
            cols: [[
				{field:'id', width:90, title:'ID', align: 'center'},
                {field: 'name', width: 150, title: '名称', align: 'center'},
                {field: 'spec_value', minWidth: 240, title: '属性值',templet:function(data){
                    var html="";
                    if(data.spec_value.length>0){
                        $.each(data.spec_value,function(i,j){
                            html = html + "|"+ j.value;
                        });
                        html=html.substr(1);
                        return html;
                    }else{
                        return '无属性值';
                    }
                }, align: 'center'},
                {field: 'sort', width: 100, title: '排序', align: 'center'},
                {field: 'operating', title: '操作', templet: function (data) {
                    var html = '<a  class="layui-btn layui-btn-xs edit-class" data-id="' + data.id + '">编辑</a>';
                    html += '<a class="layui-btn layui-btn-danger layui-btn-xs del-class" data-name="'+data.name+'" data-id="' + data.id + '">删除</a>';
                    return html;
                }, align: 'center', width: 140}
            ]],
        });

        layui.form.on('submit(*)', function(data){
            layui.table.reload('userTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //添加属性
        $(document).on('click', '.add-class', function(){
            var id = $(this).attr('data-id');
            JsGet('{:url("GoodsTypeSpec/add")}',function (e) {
				if(e.status){
					window_box = layer.open({
						type: 1,
						title: '添加属性',
						area: setpage(420,420), //宽高
						content: e.data,
                        btn: ['保存', '关闭'],
                        yes: function () {
                            var formData = form.val("add_goods_type_spec");
                            if (!formData) {
                                layer.msg('请先完善数据', { time: 1300 });
                                return false;
                            }
                            JsPost('{:url("GoodsTypeSpec/add")}', formData, function (e) {
                                if (e.status === true) {
                                    layer.close(window_box);
                                    layer.msg(e.msg, { time: 1300 }, function () {
                                        table.reload('typeTable');
                                    });
                                } else {
                                    layer.msg(e.msg, { time: 1300 });
                                }
                            });

                        }
					});
				}else{
					layer.msg(e.msg);
				}
            });
            return false;
        });

        //编辑属性
        $(document).on('click', '.edit-class', function(){
            var id = $(this).attr('data-id');
            JsGet('{:url("GoodsTypeSpec/edit")}?id='+id, function(e){
				if (e.status) {
                    window_box = layer.open({
                        type: 1,
                        title: '编辑属性',
                        area: setpage(440, 550), //宽高
                        content: e.data,
                        btn: ['保存', '关闭'],
                        yes: function () {
                            var formData = form.val("edit_goods_type_spec");
                            if (!formData) {
                                layer.msg('请先完善数据', { time: 1300 });
                                return false;
                            }
                            JsPost('{:url("GoodsTypeSpec/edit")}', formData, function (e) {
                                if (e.status === true) {
                                    layer.close(window_box);
                                    layer.msg(e.msg, { time: 1300 }, function () {
                                        table.reload('typeTable');
                                    });
                                } else {
                                    layer.msg(e.msg, { time: 1300 });
                                }
                            });

                        }
                    });
                } else {
                    layer.msg(e.msg);
                }
			});
            return false;
        });

        //删除分类
        $(document).on('click', '.del-class', function(){
            var id = $(this).attr('data-id');
            var name = $(this).attr('data-name');
            layer.confirm('确认删除属性：'+name+' 吗？', {
                title: '提示', btn: ['确认', '取消'] //按钮
            }, function(){
                delClass(id);
            });
        });

        //删除操作
        function delClass(id){
        	JsPost('{:url("GoodsTypeSpec/del")}', {'id': id}, function(e){
				layer.msg(e.msg, {time: 1300}, function(){
					table.reload('typeTable');
				});
			});
        }
    });



</script>